﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp2.Score;
using WindowsFormsApp2.Util;
using WindowsFormsApp2.VTO;

namespace WindowsFormsApp2
{
    public partial class ResultForm : Form
    {
        public ResultForm()
        {
            InitializeComponent();
        }

        public string msg { get; internal set; }
        public string file_img { get; internal set; }


        RegUtil regUtil = new RegUtil();

        const string last_user = "last_user";
        const string last_gx = "last_gx";

        private void ResultForm_Load(object sender, EventArgs e)
        {
            for(int ixxx = 1; ixxx < 21; ixxx++)
            {
                listView1.Items.Add(ixxx.ToString());
            }

            listView1.FullRowSelect = true;

            ReqResult req = JsonConvert.DeserializeObject<ReqResult>(msg);

            int h = req.data.h;

            int w = req.data.w;

            var img = Image.FromFile(file_img);

            Graphics g = Graphics.FromImage(img);

            Brush redBrush = new SolidBrush(Color.Red);
            Brush greenBrush = new SolidBrush(Color.Green);
            Pen redPen = new Pen(redBrush, 1);
            Pen greenPen = new Pen(greenBrush,1);

            int iTop = findTop(req.data.find_points);
            int i = 0;
            int point_i = 0;
            List<List<VTO.Point>> point_array = req.data.point_array;

            //foreach (var pt in req.data.find_points)
            //{
            //    g.DrawRectangle(redPen,
            //        pt.x, pt.y, w, h
            //        );

                

            //    i++;
            //    point_i++;
            //}

            foreach(var pt_a in req.data.point_array)
            {
                int array_size = pt_a.Count;
                Pen drawPen = array_size == 4 ? redPen : greenPen;
                Brush drawBrush = array_size == 4 ? redBrush : greenBrush;

                foreach(var ptx in pt_a)
                {
                    g.FillRectangle(drawBrush,
                        ptx.y,
                        ptx.x,
                        w,
                        h);
                }


            }

            int lv_index = 0;

            foreach (var score in req.data.score_array)
            {
                //textBox1.Text = textBox1.Text + score.ToString() + Environment.NewLine;
                listView1.Items[lv_index].SubItems.Add(score.ToString());
                g.DrawString(score.ToString(),
                    new Font("Arial", 10, FontStyle.Bold),
                    redBrush,
                    new System.Drawing.Point(2500,iTop));

                iTop += 42;

                lv_index++;
            }

            pictureBox1.Image = img;

            if (req.data.total_point_count == 80)
            {
                label1.Text = "完全识别！";
                label1.ForeColor = Color.Green;
            }
            else
            {
                label1.Text = "存在异常状况，请双击编辑！识别个数为："+req.data.total_point_count;
                label1.ForeColor = Color.Red;
            }

            
        }


        private int findTop(List<VTO.Point> points)
        {
            int iRet = 10000;

            foreach(var pt in points)
            {
                if (pt.y < iRet)
                {
                    iRet = pt.y;
                }
            }

            return iRet;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var user_name = tb_username.Text.Trim();

            var guangxi = cbb_shenfen.Text;

            if(user_name.Length==0 || guangxi.Length == 0)
            {
                MessageBox.Show("姓名和关系为空,无法继续！");
                return;
            }

            foreach(ListViewItem item in listView1.Items)
            {
                if(item.SubItems.Count==0)
                {
                    MessageBox.Show("当前存在空值，请编辑以后再保存！");
                    return;
                }
            }

            //var lines = textBox1.Lines;

            List<String> str_lines = new List<string>();

            foreach (ListViewItem item in listView1.Items)
            {
                str_lines.Add(item.SubItems[1].Text);
            }

            string total_line_score = String.Join(",", str_lines);

            string final_line = $"{user_name},{guangxi},{total_line_score}";

            string CSV_Name = AppUtil.GetAppDir() + @"Result\result.csv";

            File.AppendAllLines(CSV_Name,new string[] { final_line },Encoding.Default);

            regUtil.WriteString(last_user, user_name);
            regUtil.WriteString(last_gx, cbb_shenfen.Text);

            MessageBox.Show("入库成功，请做好入库标记！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var user_name = regUtil.ReadString(last_user);
            var last_gx_t = regUtil.ReadString(last_gx);

            tb_username.Text = user_name;
            cbb_shenfen.Text = last_gx_t;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                var selItem = listView1.SelectedItems[0];

                new EditScore
                {
                    m_lv = selItem,
                    StartPosition = FormStartPosition.CenterScreen,
                    Left = listView1.Left
                }.ShowDialog();
            }
        }
    }
}
